# 申明一个工具类
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class YygeUtils:
    # def __init__(self):
    #     # print("GetUtils被实例化了")
    #     pass
    @staticmethod
    def fileExists(path,fun1,fun2):
       # 获取当前工作目录
       workPath = os.getcwd()
       allPath = workPath + path
       if os.path.exists(allPath):
        return fun1(allPath)
       else:
        return fun2(allPath)

    @staticmethod
    def writeText(path,text):
        # 获取当前工作目录
        workPath = os.getcwd()
        allPath = workPath + path
        with open(allPath, 'w',encoding="utf-8") as file:
          file.write(text)

    # 读取文本内容 提供一个路径进来 返回这个路径的文本内容
    @staticmethod
    def getText(path):
        # 获取当前工作目录
        workPath = os.getcwd()
        jsPath = workPath + path
        print("GetFile:",jsPath)
        with open(jsPath, 'r', encoding='utf-8') as file:
            text = file.read()
        return text

    # 生成随机的WebView 随机的UA 返回的 driver
    @staticmethod
    def getWebView():
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option('useAutomationExtension', False)     # 防止检测
        chrome_options.add_argument("--mute-audio")  # 静音
        # 禁止全屏
        # chrome_options.add_argument('--start-maximized')
        chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])  # 防止检测、禁止打印日志
        chrome_options.add_argument('--disable-blink-features=AutomationControlled')
        chrome_options.add_argument('--ignore-certificate-errors')  # 忽略证书错误
        chrome_options.add_argument('--ignore-ssl-errors')  # 忽略ssl错误
        chrome_options.add_argument('–log-level=3')
        # chrome_options.add_argument("--start-maximized")
        chrome_options.add_experimental_option("detach", True)
        chrome_options.add_argument('--remote-debugging-port=9222')
        driver = webdriver.Chrome(options=chrome_options)
        return driver
        # driver. Get('https://example.com')
        # chrome_options = webdriver.ChromeOptions()
        # chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:8080")
        # driver = webdriver.Chrome(options=chrome_options,executable_path='C:\Program Files\Google\Chrome\Application\chrome.exe')
        # return driver


